<?php
namespace app\warehouse\admin;
use app\system\admin\Admin;
use app\warehouse\model\Warehouse as WarehouseModel;
use app\shop\model\Shop as ShopModel;
use app\warehouse\model\Stock as StockModel;
use app\product\model\Product as ProductModel;
use app\user\model\User as UserModel;
use think\Db;
class Stock extends Admin
{
    protected $hisiModel = 'Stock';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名


    protected function initialize()
    {
        parent::initialize();
        $users = UserModel::field('id,nick')->all();
        $this->assign('users', $users);
        $products = ProductModel::field('id,name')->all();
        $this->assign('products', $products);
        // 库存单明细类型
        $StockModel = new StockModel;
        $detailed_types = $StockModel->detailedTypes();
        $this->assign('detailed_types', $detailed_types);
        if(session('admin_type') == '管理员'){
            $wares = Db::name('warehouse')->field('id,name')->select();
        }else{
            $wares = Db::name('warehouse')->where('shop_id',session('shop_id'))->field('id,name')->select();
        }
        // $wares = Db::name('warehouse')->field('id,name')->select();
        $this->assign('wares',$wares);
    }

    /**
     * [index 仓库明细]
     * @return [type] [description]
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $where['status'] = 1;
            $where['shop_id'] = session('shop_id');

            $user_id      = $this->request->param('user_id/d', '');
            $product_id      = $this->request->param('product_id/d', '');
            $detailed_type      = $this->request->param('detailed_type/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            if(!empty($warehouse_id)){
                $where['warehouse_id'] = $warehouse_id;
            }
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!empty($product_id)){
                $where['product_id'] = $product_id;
            }
            if(!empty($detailed_type)){
                $where['detailed_type'] = $detailed_type;
            }
            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }

            $data['data'] = StockModel::where($where)
                            ->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
                            ->whereTime('createtime', [strtotime($start), strtotime($end)])
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['warehouse_name'] = Db::name('warehouse')->where('id',$value['warehouse_id'])->value('name');
            }
            $data['count'] = StockModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }
    /**
     *  实时库存
     */
    public function real()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $product_id      = $this->request->param('product_id/d', '');
            if(!empty($product_id)){
                $where['id'] = $product_id;
            }
            // $where['status'] = 1;
            // $where['shop_id'] = session('shop_id');
            $data['data'] = ProductModel::where($where)
                            ->field('*, unit_id as unit_text, decomposition_id as decomposition_text,id as warehousing')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $out_of = [];
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['zong'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    ->where('s.shop_id',session('shop_id'))
                    ->where('w.type',0)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
                $data['data'][$key]['pei'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    ->where('s.shop_id',session('shop_id'))
                    ->where('w.type',1)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
            }
            $data['count'] = ProductModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    /**
     * [decompose 配料库明细]
     * @return [type] [description]
     */
    public function decompose()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $user_id      = $this->request->param('user_id/d', '');
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            $detailed_type      = $this->request->param('detailed_type/d', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!empty($product_id)){
                $where['product_id'] = $product_id;
            }
            if(!empty($detailed_type)){
                $where['detailed_type'] = $detailed_type;
            }
            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }

            $where['status'] = 1;
            $where['shop_id'] = session('shop_id');
            // halt($where);
            // $warehouse = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 1)->find();
            // $where['warehouse_id'] = $warehouse['id'];

            if(!empty($warehouse_id)){
                $where['warehouse_id'] = $warehouse_id;
            }else{
                // 单仓库改多仓库   2020-04-15
                $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 1)->column('id');
                $where['warehouse_id'] = $warehouses;
            }

            $data['data'] = StockModel::where($where)
                            ->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
                            ->whereTime('createtime', [strtotime($start), strtotime($end)])
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['warehouse_name'] = Db::name('warehouse')->where('id',$value['warehouse_id'])->value('name');
            }
            $data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch('index');
    }
    /**
     * [always 总库明细]
     * @return [type] [description]
     */
    public function always()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);

            $user_id      = $this->request->param('user_id/d', '');
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            $detailed_type      = $this->request->param('detailed_type/d', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!empty($product_id)){
                $where['product_id'] = $product_id;
            }
            if(!empty($detailed_type)){
                $where['detailed_type'] = $detailed_type;
            }
            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }
            if(!empty($warehouse_id)){
                $where['warehouse_id'] = $warehouse_id;
            }else{
                // 单仓库改多仓库   2020-04-15
                $warehouses = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 0)->column('id');
                $where['warehouse_id'] = $warehouses;
            }

            $where['status'] = 1;
            $where['shop_id'] = session('shop_id');
            // 单仓库改多仓库   2020-04-15

            // $warehouse = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 0)->find();

            $data['data'] = StockModel::where($where)
                            ->field('*, detailed_type as detailed_type_text,user_id as user_name')
                            ->whereTime('createtime', [strtotime($start), strtotime($end)])
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();

            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['warehouse_name'] = Db::name('warehouse')->where('id',$value['warehouse_id'])->value('name');
            }
            $data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch('index');
    }

    /**
     * [realinventory 配料库实时库存]
     * @return [type] [description]
     */
    public function realinventory()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $where2      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            if(!empty($product_id)){
                $where['id'] = $product_id;
            }
            $warehouse_info = Db::name('warehouse')->where('shop_id',session('shop_id'))->where('type',1)->find();
            if(!empty($warehouse_id)){
                $where2['s.warehouse_id'] = $warehouse_id;
            }else{
                $where2['s.warehouse_id'] = $warehouse_info['id'];
            }
            if(session('admin_type') == '管理员'){

            }else{
                $where2['s.shop_id'] = session('shop_id');
            }
            $data['data'] = ProductModel::where($where)
                            ->field('*, unit_id as unit_text, decomposition_id as decomposition_text')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['in'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','入库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
                $data['data'][$key]['in_price'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','入库')
                    ->where('s.status',1)
                    ->sum('s.price');
                $data['data'][$key]['out'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
                $data['data'][$key]['out_price'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.price');
            }
            $data['count'] = ProductModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        if(session('admin_type') == '管理员'){
            $wares = Db::name('warehouse')->field('id,name')->where('type',1)->select();
        }else{
            $wares = Db::name('warehouse')->where('shop_id',session('shop_id'))->where('type',1)->field('id,name')->select();
        }

        $this->assign('wareZong',$wares);
        return $this->fetch();
    }
    /**
     * [realzong 总库实时库存]
     * @return [type] [description]
     */

    public function realzong()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $where2      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            if(!empty($product_id)){
                $where['id'] = $product_id;
            }
            $warehouse_info = Db::name('warehouse')->where('shop_id',session('shop_id'))->where('type',0)->find();
            if(!empty($warehouse_id)){
                $where2['s.warehouse_id'] = $warehouse_id;
            }else{
                $where2['s.warehouse_id'] = $warehouse_info['id'];
            }
            if(session('admin_type') == '管理员'){

            }else{
                $where2['s.shop_id'] = session('shop_id');
            }
            $data['data'] = ProductModel::where($where)
                            ->field('*, unit_id as unit_text, decomposition_id as decomposition_text')
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            foreach ($data['data'] as $key => $value) {
                // 入库产品数量
                $data['data'][$key]['in'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','入库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
                // 入库产品总金额
                $data['data'][$key]['in_price'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','入库')
                    ->where('s.status',1)
                    ->sum('s.price');
                // 出库产品数量
                $data['data'][$key]['out'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.product_num');
                // 出库产品总金额
                $data['data'][$key]['out_price'] = Db::name('stock')->alias('s')
                    ->join('hisi_warehouse w', 'w.id=s.warehouse_id')
                    // ->where('s.shop_id',session('shop_id'))
                    ->where($where2)
                    ->where('s.product_id',$value['id'])
                    ->where('s.type','出库')
                    ->where('s.status',1)
                    ->sum('s.price');
            }
            $data['count'] = ProductModel::where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        if(session('admin_type') == '管理员'){
            $wares = Db::name('warehouse')->field('id,name')->where('type',0)->select();
        }else{
            $wares = Db::name('warehouse')->where('shop_id',session('shop_id'))->where('type',0)->field('id,name')->select();
        }

        $this->assign('wareZong',$wares);
        return $this->fetch();
    }

}
